package com.lyb.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lyb
 * @create 2022/3/12-11:08
 * @method 测试jedis
 */

public class JedisDemo1 {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.31.235",6379);
        String ping = jedis.ping();
        System.out.println("ping = " + ping);
    }

    // 操作字符串String
    @Test
    public void testString(){
        Jedis jedis = new Jedis("192.168.31.235",6379);

        String set = jedis.set("name", "lyb");
        System.out.println("set = " + set);
        System.out.println("------------------------------");

        //设置多个key
        jedis.mset("k1", "v1", "k2", "v2");
        List<String> mget = jedis.mget("k1", "k2");
        System.out.println("mget = " + mget);

        System.out.println("------------------------------");
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println("key = " + key);
        }
    }

    // 操作List
    @Test
    public void testList(){
        Jedis jedis = new Jedis("192.168.31.235",6379);

        jedis.lpush("list1", "aaa", "bbb", "ccc");
        List<String> list1 = jedis.lrange("list1", 0, -1);
        System.out.println(list1);

    }

    // 操作Set
    @Test
    public void testSet(){
        Jedis jedis = new Jedis("192.168.31.235", 6379);
        jedis.sadd("set1", "set1", "set2", "set3");
        Set<String> set1 = jedis.smembers("set1");
        System.out.println(set1);
    }

    // 操作Hash
    @Test
    public void testHash(){
        Jedis jedis = new Jedis("192.168.31.235", 6379);
        Map<String, String> user = new HashMap<>();
        user.put("name", "lyb");
        user.put("age", "24");
        user.put("sex", "male");
        jedis.hset("hash1", user);
        String hget = jedis.hget("hash1", "name");
        System.out.println("hget = " + hget);
    }

    // 操作Zset
    @Test
    public void testZset(){
        Jedis jedis = new Jedis("192.168.31.235", 6379);
        jedis.zadd("zset1", 50d, "java");
        List<String> zset1 = jedis.zrange("zset1", 0, -1);
        System.out.println(zset1);
    }


}
